package com.qqt.csr.workbench.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.qqt.csr.common.acl.aics.AICustomerServiceClient;
import com.qqt.csr.common.acl.aics.req.QueryingCsMemberDTO;
import com.qqt.csr.common.acl.aics.resp.CsMemberInfoDTO;
import com.qqt.csr.common.exception.StatusCode;
import com.qqt.csr.common.utils.ServiceAssert;
import com.qqt.csr.common.vo.req.PageRequest;
import com.qqt.csr.common.vo.resp.PageResponse;
import com.qqt.csr.workbench.entity.SkillSet;
import com.qqt.csr.workbench.entity.SkillSetMember;
import com.qqt.csr.workbench.mapper.SkillSetMemberMapper;
import com.qqt.csr.workbench.vo.req.QueryingSkillSetMemberReqVO;
import com.qqt.csr.workbench.vo.resp.SkillSetMemberInfoRespVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SkillSetMemberService extends ServiceImpl<SkillSetMemberMapper, SkillSetMember> {

    @Autowired
    private SkillSetService skillSetService;
    @Autowired
    private AICustomerServiceClient aiCustomerServiceClient;
    @Autowired
    private RedissonClient redissonClient;

    /**
     * 根据客服id删除技能组成员
     *
     * @param csIdList
     */
    public void deleteBy(String tenantId, Long skillSetId, List<Long> csIdList) {
        if (CollectionUtils.isEmpty(csIdList)) {
            return;
        }
        SkillSet skillSet = skillSetService.queryById(skillSetId, tenantId);
        ServiceAssert.notNull(skillSet, StatusCode.Common.NOT_EXIST.getCode(), "当前技能组不存在，删除成员失败");

        this.lambdaUpdate().set(SkillSetMember::getDeleteFlag, 1)
                .eq(SkillSetMember::getSkillSetId, skillSetId)
                .in(SkillSetMember::getCsId, csIdList)
                .update();

    }

    /**
     * 添加技能组成员
     *
     * @param csIdList
     * @param skillSetId
     */
    public void addSkillSetMember(String tenantId, List<Long> csIdList, Long skillSetId, Long creatorId) {
        if (CollectionUtils.isEmpty(csIdList)) {
            return;
        }

        SkillSet skillSet = skillSetService.queryById(skillSetId, tenantId);
        ServiceAssert.notNull(skillSet, StatusCode.Common.NOT_EXIST.getCode(), "当前技能组不存在，添加成员失败");

        List<Long> existCsIdList = this.lambdaQuery().select(SkillSetMember::getCsId)
                .eq(SkillSetMember::getSkillSetId, skillSetId)
                .in(SkillSetMember::getCsId, csIdList)
                .eq(SkillSetMember::getDeleteFlag, 0)
                .list().stream().map(SkillSetMember::getCsId).distinct().collect(Collectors.toList());
        csIdList.removeAll(existCsIdList);
        if (CollectionUtils.isEmpty(csIdList)) {
            return;
        }

        List<SkillSetMember> skillSetMemberList = Lists.newArrayList();
        csIdList.forEach(csId -> {
            SkillSetMember skillSetMember = SkillSetMember.builder()
                    .deleteFlag(0)
                    .creatorId(creatorId)
                    .csId(csId)
                    .skillSetId(skillSetId)
                    .build();
            skillSetMemberList.add(skillSetMember);
        });
        this.baseMapper.batchInsert(skillSetMemberList);
    }

    /**
     * 查询技能组成员分页信息
     *
     * @param req
     * @return
     */
    public PageResponse<SkillSetMemberInfoRespVO> queryPage(PageRequest<QueryingSkillSetMemberReqVO> req, String tenantId) {
        SkillSet skillSet = skillSetService.queryById(req.getData().getSkillSetId(), tenantId);
        if (skillSet == null) {
            return new PageResponse<>();
        }

        List<Long> csIdList = this.lambdaQuery().select(SkillSetMember::getCsId)
                .eq(SkillSetMember::getSkillSetId, req.getData().getSkillSetId())
                .eq(SkillSetMember::getDeleteFlag, 0)
                .in(CollectionUtils.isNotEmpty(req.getData().getCsIdList()), SkillSetMember::getCsId, req.getData().getCsIdList())
                .list()
                .stream()
                .map(SkillSetMember::getCsId)
                .distinct()
                .collect(Collectors.toList());

        if (CollectionUtils.isEmpty(csIdList)) {
            return new PageResponse<>(0L, req.getPageSize(), req.getPage(), Lists.newArrayList());
        }


        QueryingCsMemberDTO remoteReq = new QueryingCsMemberDTO();
        remoteReq.setUserIdList(csIdList)
                .setAccount(req.getData().getAccount())
                .setAccountStatus(req.getData().getAccountStatus())
                .setNickName(req.getData().getNickName())
                .setRoleId(req.getData().getRoleId())
                .setEmployeeNo(req.getData().getEmployeeNo())
                .setPageNum(req.getPage())
                .setPageSize(req.getPageSize());

        PageResponse<CsMemberInfoDTO> remoteResp = aiCustomerServiceClient.queryCsMemberList(remoteReq);
        List<SkillSetMemberInfoRespVO> skillSetMemberInfoRespVOList = remoteResp.getData().getList()
                .stream().map(item ->
                        new SkillSetMemberInfoRespVO()
                                .setSkillSetId(req.getData().getSkillSetId())
                                .setCsId(item.getUserId())
                                .setAccount(item.getUserName())
                                .setAccountStatus(item.getStatus())
                                .setNickName(item.getNickName())
                                .setEmployeeNo(item.getEmployeeNo())
                                .setRoleName(item.getRoleName())
                                .setDeptName(item.getDeptName())
                )
                .collect(Collectors.toList());

        return new PageResponse<>(remoteResp.getData().getTotal(),
                remoteResp.getData().getPageSize(),
                remoteResp.getData().getPage(),
                skillSetMemberInfoRespVOList
        );
    }

    public List<Long> checkMemberList(Long skillSetId, List<Long> csIdList) {
        if (CollectionUtils.isEmpty(csIdList)) {
            return Lists.newArrayList();
        }
        return this.lambdaQuery().select(SkillSetMember::getCsId)
                .eq(SkillSetMember::getSkillSetId, skillSetId)
                .in(SkillSetMember::getCsId, csIdList)
                .eq(SkillSetMember::getDeleteFlag, 0)
                .list()
                .stream()
                .map(SkillSetMember::getCsId)
                .distinct()
                .collect(Collectors.toList());
    }

    /**
     * 获取客服所在所有技能组id
     *
     * @param csId
     * @return
     */
    public List<Long> getCsSkillSetIdList(Long csId) {
        return this.lambdaQuery().select(SkillSetMember::getSkillSetId)
                .eq(SkillSetMember::getCsId, csId)
                .eq(SkillSetMember::getDeleteFlag, 0)
                .list().stream().map(SkillSetMember::getSkillSetId).distinct().collect(Collectors.toList());
    }

    /**
     * 获取技能组下的所有成员
     *
     * @param skillSetId
     * @return
     */
    public List<Long> getCsIdBySkillSetId(Long skillSetId) {
        return this.lambdaQuery().select(SkillSetMember::getCsId)
                .eq(SkillSetMember::getSkillSetId, skillSetId)
                .eq(SkillSetMember::getDeleteFlag, 0)
                .list().stream().map(SkillSetMember::getCsId).distinct().collect(Collectors.toList());
    }
}
